from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_cors import CORS
from flask_session import Session
from .config import Config
from dotenv import load_dotenv

load_dotenv()

db = SQLAlchemy()

def create_app():
    app = Flask(__name__, static_folder="static")
    app.config.from_object(Config)

    # ✅ CORS 设置（允许前端访问并传递 cookies）
    CORS(app, supports_credentials=True, origins=["http://localhost:5173"])

    # ✅ Session 设置（需要配置在 config.py 中）
    Session(app)

    db.init_app(app)

    from .routes import bp as routes_bp
    app.register_blueprint(routes_bp, url_prefix="/")

    from . import models

    return app
